package org.blame.rabbitmqadvancedfeature.listener;


import com.rabbitmq.client.Channel;
import org.blame.rabbitmqadvancedfeature.constant.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;


import java.util.Date;

@Component
public class QosListener {
    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void delayListener1(Message message, Channel channel) throws Exception{
        try {
            System.out.println("收到消息：" + message);

            // 手动确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            System.out.println("1111111");
        } catch (Exception e) {
            // 拒绝消息并重新入队
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            System.err.println("处理失败，消息重新入队：" + e.getMessage());

        }

    }
    @RabbitListener(queues = Constants.QOS_QUEUE)
    public void delayListener2(Message message, Channel channel) throws Exception {
        try {
            System.out.println("收到消息：" + message);

            // 手动确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            System.out.println("22222222222");
        } catch (Exception e) {
            // 拒绝消息并重新入队
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            System.err.println("处理失败，消息重新入队：" + e.getMessage());

        }
    }
}
